package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strings"
)

var angelMap = map[string]string{
	"E'": "偏东",
	"NE": "东北",
	"N'": "偏北",
	"NW": "西北",
	"W'": "偏西",
	"SW": "西南",
	"S'": "偏南",
	"SE": "东南",
}

var angelMap1 = map[string]string{
	"E":  "东",
	"NE": "东北",
	"N":  "北",
	"NW": "西北",
	"W":  "西",
	"SW": "西南",
	"S":  "南",
	"SE": "东南",
}

// HandelYdyl
func HandelYdyl(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]

	var foreList models.ForeList
	db.Where("code = ?", "rahx").First(&foreList)
	var foreDetail models.ForeDetailJson
	db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, time, "0").First(&foreDetail)
	var raData = models.Forecast{}
	json.Unmarshal(foreDetail.Data, &raData)

	foreListRayg := GetForeListByCode("rayg", db)
	var foreDetailRayg models.ForeDetailJson
	db.Where("foreListId = ? AND time = ? AND state != ?", foreListRayg.Id, time, "0").First(&foreDetailRayg)
	var raygData = models.Forecast{}
	json.Unmarshal(foreDetailRayg.Data, &raygData)
	for i, report := range forecast.Reports {
		if i == 0 {
			for j := 1; j < 4; j++ {
				report.Data[5][2*j].Value = tools.IntToStr(j)
				report.Data[6][2*j].Value = tools.IntToStr(j)
				_, m, d := tools.GetNextDay(year, month, day, j)
				report.Data[0][2*j-1].Value = m + "月" + d + "日"
				if foreDetail.State != "0" && len(raData.Reports) > 0 {
					//处理风速的关系
					//windArr := strings.Split(raData.Reports[0].Data[j][2].Value, "▽")
					var windStr string
					numArr := tools.GetNum(raData.Reports[0].Data[j][2].Value)
					if len(numArr) == 2 {
						if strings.Contains(raData.Reports[0].Data[j][2].Value, "▽") {
							windStr = tools.IntToStr(numArr[0]-1) + "级阵风" + tools.IntToStr(numArr[1]-1) + "级"
						} else if strings.Contains(raData.Reports[0].Data[j][2].Value, "-") {
							windStr = tools.IntToStr(numArr[0]-1) + "-" + tools.IntToStr(numArr[1]-1) + "级"
						}
					} else if len(numArr) == 1 {
						windStr = tools.IntToStr(numArr[0]-1) + "级"
					} else if len(numArr) > 2 {
						windStr = tools.ReplaceNum(raData.Reports[0].Data[j][2].Value, -1)
					}
					//if len(windArr) == 2 {
					//	if len(numArr) == 2 {
					//		windStr = tools.IntToStr(numArr[0]-1) + "级阵风" + tools.IntToStr(numArr[1]-1) + "级"
					//	} else if len(numArr) == 3 {
					//		windStr = tools.IntToStr(numArr[0]-1) + "-" + tools.IntToStr(numArr[1]-1) + "级阵风" + tools.IntToStr(numArr[2]-1) + "级"
					//	}
					//} else {
					//	if len(numArr) == 1 {
					//		windStr = tools.IntToStr(numArr[0]-1) + "级"
					//	} else if len(numArr) == 2 {
					//		windStr = tools.IntToStr(numArr[0]-1) + "-" + tools.IntToStr(numArr[1]-1) + "级"
					//	}
					//}
					//处理浪高
					var waveStr string
					if foreDetailRayg.Id != 0 {
						waveArr := strings.Split(raygData.Reports[1].Data[j][4].Value, "米")
						if len(waveArr) == 2 {
							waveStr = tools.Float64ToFixedStr(tools.StrToFloat64(waveArr[0])-0.2, 1)
						} else if len(waveArr) > 2 {
							numArr1 := tools.GetNumFloat64(raygData.Reports[1].Data[j][4].Value)
							tempStr := strings.Split(strings.Split(raygData.Reports[1].Data[j][4].Value, "，")[1], "到")[0]
							tempStr = strings.Replace(tempStr, "明天", "", -1)
							tempStr = strings.Replace(tempStr, "大后天", "", -1)
							tempStr = strings.Replace(tempStr, "后天", "", -1)
							waveStr = tools.Float64ToFixedStr(numArr1[0]-0.2, 1) + "，" + tempStr + "到" + tools.Float64ToFixedStr(numArr1[1]-0.2, 1)
							//fmt.Println(numArr1)
							//numArr1 := tools.GetNum(raData.Reports[0].Data[j][4].Value)
							//zw := strings.Split(strings.Split(raData.Reports[0].Data[j][4].Value, "天")[1], "到")[0]
							//num1 := tools.StrToFloat64(tools.IntToStr(numArr1[0])+"."+tools.IntToStr(numArr1[1])) - 0.2
							//num2 := tools.StrToFloat64(tools.IntToStr(numArr1[2])+"."+tools.IntToStr(numArr1[3])) - 0.2
							//waveStr = tools.Float64ToFixedStr(num1, 1) + "，" + zw + "到" + tools.Float64ToFixedStr(num2, 1)
						}
					}

					windAngleArr := strings.Split(raData.Reports[0].Data[j][3].Value, "转")
					if len(windAngleArr) == 2 {
						report.Data[1][2*j-1].Value = angelMap[windAngleArr[0]] + "转" + angelMap[windAngleArr[1]]
					} else if len(windAngleArr) == 1 {
						report.Data[1][2*j-1].Value = angelMap[raData.Reports[0].Data[j][3].Value]
					}

					windAngleArr = strings.Split(raData.Reports[0].Data[j][3].Value, "到")
					if len(windAngleArr) == 2 {
						report.Data[1][2*j-1].Value = angelMap1[windAngleArr[0]] + "到" + angelMap1[windAngleArr[1]]
					}

					report.Data[2][2*j-1].Value = windStr
					waveAngleArr := strings.Split(raData.Reports[0].Data[j][6].Value, "转")
					if len(waveAngleArr) == 2 {
						report.Data[3][2*j-1].Value = angelMap[waveAngleArr[0]] + "转" + angelMap[waveAngleArr[1]]
					} else if len(waveAngleArr) == 1 {
						report.Data[3][2*j-1].Value = angelMap[raData.Reports[0].Data[j][6].Value]
					}

					waveAngleArr = strings.Split(raData.Reports[0].Data[j][6].Value, "到")
					if len(waveAngleArr) == 2 {
						report.Data[3][2*j-1].Value = angelMap1[waveAngleArr[0]] + "到" + angelMap1[waveAngleArr[1]]
					}

					report.Data[4][2*j-1].Value = waveStr
				}
			}
		} else {
			for j := 0; j < 3; j++ {
				report.Data[j+1][5].Value = tools.IntToStr(2*(j+1) - 1)
				report.Data[j+1][6].Value = tools.IntToStr(2*(j+1) - 1)

				_, m1, d1 := tools.GetNextDay(year, month, day, j)
				_, m, d := tools.GetNextDay(year, month, day, j+1)
				str := ""
				if j == 0 {
					str = m1 + "月" + d1 + "日20时至" + m + "月" + d + "日20时"
					report.Data[1][0].Value = str
				} else if j == 1 {
					str = m1 + "月" + d1 + "日20时至" + m + "月" + d + "日20时"
					report.Data[2][0].Value = str
				} else {
					str = m1 + "月" + d1 + "日20时至" + m + "月" + d + "日20时"
					report.Data[3][0].Value = str
				}
				if len(raData.Reports) == 0 {
					continue
				}
				//windArr := strings.Split(raData.Reports[0].Data[j+1][2].Value, "▽")
				//var windStr string
				//numArr := tools.GetNum(raData.Reports[0].Data[j+1][2].Value)
				//if len(windArr) == 2 {
				//	if len(numArr) == 2 {
				//		windStr = tools.IntToStr(numArr[0]) + "级阵风" + tools.IntToStr(numArr[1]) + "级"
				//	} else if len(numArr) == 3 {
				//		windStr = tools.IntToStr(numArr[0]) + "-" + tools.IntToStr(numArr[1]) + "级阵风" + tools.IntToStr(numArr[2]) + "级"
				//	}
				//} else {
				//	if len(numArr) == 1 {
				//		windStr = tools.IntToStr(numArr[0]) + "级"
				//	} else if len(numArr) == 2 {
				//		windStr = tools.IntToStr(numArr[0]) + "-" + tools.IntToStr(numArr[1]) + "级"
				//	} else {
				//		//windStr = raData.Reports[0].Data[j+1][2].Value
				//	}
				//}

				var windStr string
				numArr := tools.GetNum(raData.Reports[0].Data[j+1][2].Value)
				if len(numArr) == 2 {
					if strings.Contains(raData.Reports[0].Data[j+1][2].Value, "▽") {
						windStr = tools.IntToStr(numArr[0]) + "级阵风" + tools.IntToStr(numArr[1]) + "级"
					} else if strings.Contains(raData.Reports[0].Data[j+1][2].Value, "-") {
						windStr = tools.IntToStr(numArr[0]) + "-" + tools.IntToStr(numArr[1]) + "级"
					}
				} else if len(numArr) == 1 {
					windStr = tools.IntToStr(numArr[0]) + "级"
				} else if len(numArr) > 2 {
					windStr = tools.ReplaceNum(raData.Reports[0].Data[j+1][2].Value, 0)
				}

				waveArr := strings.Split(raData.Reports[0].Data[j+1][4].Value, "米")
				var waveStr string
				if len(waveArr) == 2 {
					waveStr = tools.Float64ToFixedStr(tools.StrToFloat64(waveArr[0]), 1)
				} else if len(waveArr) > 2 {
					numArr1 := tools.GetNum(raData.Reports[0].Data[j+1][4].Value)
					zw := strings.Split(strings.Split(raData.Reports[0].Data[j+1][4].Value, "天")[1], "到")[0]
					num1 := tools.StrToFloat64(tools.IntToStr(numArr1[0]) + "." + tools.IntToStr(numArr1[1]))
					num2 := tools.StrToFloat64(tools.IntToStr(numArr1[2]) + "." + tools.IntToStr(numArr1[3]))
					waveStr = tools.Float64ToFixedStr(num1, 1) + "，" + zw + "到" + tools.Float64ToFixedStr(num2, 1)
				}
				winAngleArr := strings.Split(raData.Reports[0].Data[j+1][3].Value, "转")
				if len(winAngleArr) == 2 {
					report.Data[j+1][1].Value = angelMap[winAngleArr[0]] + "转" + angelMap[winAngleArr[1]]
				} else if len(winAngleArr) == 1 {
					report.Data[j+1][1].Value = angelMap[raData.Reports[0].Data[j+1][3].Value]
				}

				winAngleArr = strings.Split(raData.Reports[0].Data[j+1][3].Value, "到")
				if len(winAngleArr) == 2 {
					report.Data[j+1][1].Value = angelMap1[winAngleArr[0]] + "到" + angelMap1[winAngleArr[1]]
				}
				report.Data[j+1][2].Value = windStr
				waveAngleArr := strings.Split(raData.Reports[0].Data[j+1][6].Value, "转")
				if len(waveAngleArr) == 2 {
					report.Data[j+1][3].Value = angelMap[waveAngleArr[0]] + "转" + angelMap[waveAngleArr[1]]
				} else if len(winAngleArr) == 1 {
					report.Data[j+1][3].Value = angelMap[raData.Reports[0].Data[j+1][6].Value]
				}

				waveAngleArr = strings.Split(raData.Reports[0].Data[j+1][6].Value, "到")
				if len(waveAngleArr) == 2 {
					report.Data[j+1][3].Value = angelMap1[waveAngleArr[0]] + "到" + angelMap1[waveAngleArr[1]]
				}
				report.Data[j+1][4].Value = waveStr
			}
		}
	}
}
